package com.ccys.netty.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;

/**
 * @author cloud fly
 * @description 描述
 * @date 2024/3/1
 */
public class WebSocketHandshakeLogger extends SimpleChannelInboundHandler<WebSocketServerProtocolHandler.HandshakeComplete> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, WebSocketServerProtocolHandler.HandshakeComplete msg) throws Exception {
        String version = msg.requestHeaders().get("Sec-WebSocket-Version");
        String key = msg.requestHeaders().get("Sec-WebSocket-Key");

        System.out.println("WebSocket handshake complete:");
        System.out.println("WebSocket Version: " + version);
        System.out.println("WebSocket Key: " + key);
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof WebSocketServerProtocolHandler.HandshakeComplete) {
            WebSocketServerProtocolHandler.HandshakeComplete handshake = (WebSocketServerProtocolHandler.HandshakeComplete) evt;
            String version = handshake.requestHeaders().get("Sec-WebSocket-Version");
            String key = handshake.requestHeaders().get("Sec-WebSocket-Key");

            System.out.println("WebSocket handshake complete:");
            System.out.println("WebSocket Version: " + version);
            System.out.println("WebSocket Key: " + key);
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }

}
